package lv.premia.katalogs;

import java.sql.SQLException;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.View.OnClickListener;
//import android.database.sqlite.SQLiteDatabase;

import lv.premia.katalogs.ItemList;
//import com.j256.ormlite.table.TableUtils;


public class DatabaseManager {

    static private DatabaseManager instance;

    static public void init(Context ctx) {
        if (null==instance) {
            instance = new DatabaseManager(ctx);
        }
    }

    static public DatabaseManager getInstance() {
        return instance;
    }

    private DatabaseHelper helper;
    private DatabaseManager(Context ctx) {
        helper = new DatabaseHelper(ctx);
    }

    private DatabaseHelper getHelper() {
        return helper;
    }

    public List<ItemList> getAllItemLists() {
        List<ItemList> itemLists = null;
        try {
            itemLists = getHelper().getItemListDao().queryForAll();
            //itemLists = getHelper().g
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return itemLists;
    }
    
    public void addItemList(ItemList l) {
    	
		try {
			//resetTables();
			getHelper().getItemListDao().create(l);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
    
    public ItemList getItemListWithId(int ItemListId) {
        ItemList ItemList = null;
        try {
            ItemList = getHelper().getItemListDao().queryForId(ItemListId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ItemList;
    }
    
    ////
    
public void refreshItemList(ItemList itemList) {
		try {
			getHelper().getItemListDao().refresh(itemList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void updateItemList(ItemList itemList) {
		try {
			getHelper().getItemListDao().update(itemList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
    ///
	
	public void resetTables(ItemList itemList) {
		// TODO Auto-generated method stub
		//Droping table itemlist
		SQLiteDatabase db = getHelper().getWritableDatabase();
		db.execSQL("DROP TABLE IF EXISTS itemlist");
		
		//recreate database empty tables
		DatabaseManager.instance.helper.onCreate(db);
	}
/*
	public static void init(OnClickListener onClickListener) {
		// TODO Auto-generated method stub
		
	}
	*/
	
	

	
}
